﻿using Che.Domain.Entity.EtityPulic;
using Che.Domain.Entity.EventsBut;
using Che.Domain.repository.Dosutdium;
using Microsoft.Extensions.Caching.Distributed;
using SqlSugar;
using System;
using System.Globalization;

namespace Che.EFCore.Repository.EStadium
{
    public class Sustadium : Dsutadium
    {
        private readonly MyDbContext dbContext;
        private readonly ISqlSugarClient sugarClient;
        private readonly IDistributedCache distributedCache;
        public Sustadium(MyDbContext dbContext, ISqlSugarClient sugarClient, IDistributedCache distributedCache)
        {
            this.dbContext = dbContext;
            this.sugarClient = sugarClient;
            this.distributedCache = distributedCache;
        }

        public Task<bool> addstadium(Stadium stadium)
        {
            sugarClient.Insertable(stadium).IgnoreColumns(ignoreNullColumn: true).ExecuteCommand();
            return Task.FromResult(true);
        }

        public async Task<PageResult<Stadium>> obscurestadiac(int pageNumber, int pageSize, string name, string date)
        {
            // 参数校验
            if (pageNumber < 1) pageNumber = 1;
            if (pageSize < 1) pageSize = 10;

            // 构建查询条件
            var query = sugarClient.Queryable<Stadium>();

            // 按名称模糊搜索
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(it => it.StadiumName.Contains(name));
            }

            // 按日期精确匹配
            if (!string.IsNullOrEmpty(date))
            {
                if (!DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsedDate))
                {
                    return new PageResult<Stadium>(new List<Stadium>(), 0, pageNumber, pageSize);
                }
                query = query.Where(it => SqlFunc.DateIsSame(it.StadiuTime, parsedDate));
            }

            // 执行分页查询（使用 ref 获取 totalCount）
            
                 int totalCount = await query.CountAsync();  // 先获取总数
            var data = await query.ToPageListAsync(pageNumber, pageSize);  // 再获取分页数据

            return new PageResult<Stadium>(data, totalCount, pageNumber, pageSize);
        }

        public async Task<PageResult<Stadium>> stadiac(int pageNumber, int pageSize)
        {
      
            int totalCount = 0;
            var data = sugarClient.Queryable<Stadium>().ToPageList(pageNumber, pageSize, ref totalCount);
            return  new PageResult<Stadium>(data, totalCount, pageNumber, pageSize);
           
        }
    }
}
